﻿using HuaCheng.Controller;
using HuaCheng.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using HuaCheng.CommonClass;

namespace HuaCheng.Views
{
    /// <summary>
    /// 通道锁定
    /// </summary>
    public partial class ChannelLock : Form
    {
        #region 全局参数
        private string devIp;//设备ip
        public int gid;//柜号
        public int mid;//中位机号
        public delegate void SetChlLock();
        public event SetChlLock ChlLock;
        public delegate List<int> GetChlStat();
        public event GetChlStat GetRunChl;
        #endregion
        #region xml读取到datagirdview
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="devTypeName">设备类型名称</param>
        /// <param name="ip">设备ip</param>
        /// <param name="devName">设备</param>
        public ChannelLock(string ip,int gid,int mid)
        {
            InitializeComponent();
            this.cbxDevType.Text = gid.ToString();
            this.cbxDevId.Text = ip;
            this.devIp = ip;
            this.gid = gid;
            this.mid = mid;
            string xmlName = "Lock-" + devIp + ".xml";//根据下拉框内容查询相应的xml文件
            string xmlPath = DicHelper.RetrieveChlLockPath() + xmlName;//xml文件路径
            LoadDefaDgv(xmlPath,ip,gid);

        }
        /// <summary>
        /// 初始加载给datagirdview绑定空表
        /// <param name="file">xml文件位置</param>
        /// <param name="id">设备id</param>
        /// </summary>
        private void LoadDefaDgv(string file,string ip,int gid)
        {
            DataSet defaultDs = ChannelLockControl.SqlFillDataSet(ip,gid,mid);//先查数据库得表结构
            defaultDs = ChannelLockControl.XmlFillDataSet(file,defaultDs);//再结合xml文件填充表中锁定信息数据
            dgvLockInfo.DataSource = defaultDs;//绑定数据源
            dgvLockInfo.DataMember = defaultDs.Tables[0].TableName;//表名
            dgvLockInfo.ClearSelection();
            if (dgvLockInfo.Rows.Count>0)//如果有通道信息才可以锁定
            {
                dgvLockInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;//宽度自动填充
                dgvLockInfo.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
                dgvLockInfo.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                dgvLockInfo.Columns[0].FillWeight = 50;//第一列占其它列宽的50%
                dgvLockInfo.Enabled = true;
                btnOK.Enabled = true;
            }
            else
            {
                dgvLockInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;//宽度自动填充
            }
        }
        #endregion
        #region 修改datagridview保存到xml
        /// <summary>
        /// 鼠标点击单元格值取反
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvLockInfo_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == -1 || e.RowIndex == -1)
                {
                    return;
                }
                if (e.Clicks < 2)//防止点击两次之后进入编辑状态
                {
                    this.dgvLockInfo.EndEdit();
                }
                List<int> runChls = GetRunChl();//触发监听并接收返回的结果
                if (e.ColumnIndex == 0)//点击第一列 ，全为0或者全为1
                {
                    if (runChls.Count == 0)
                    {
                        DataGridViewCell allCheck = this.dgvLockInfo.Rows[e.RowIndex].Cells[0];//获取第一列的选中状态
                        if (!Convert.ToBoolean(allCheck.Value))//弹窗提示
                        {
                            MessageBox.Show("锁定第" + (e.RowIndex + 1) + "层所有通道", "提示", MessageBoxButtons.OK);
                        }
                        else
                        {
                            MessageBox.Show("解锁第" + (e.RowIndex + 1) + "层所有通道", "提示", MessageBoxButtons.OK);
                        }
                        allCheck.Value = Convert.ToBoolean(allCheck.Value) ? false : true;//第一列取反
                        string setVal = Convert.ToBoolean(allCheck.Value) ? "1" : "0";//如果选中改单元格则为该行全部取1否则取0
                        DataGridViewCellCollection cells = this.dgvLockInfo.Rows[e.RowIndex].Cells;
                        for (int i = 1; i < cells.Count; i++)//从第二列开始全部取1或0
                        {
                            cells[i].Value = setVal;
                        }
                    }
                    else
                    {
                        string errMsg = "";
                        for (int i = 0; i < runChls.Count; i++)
                        {
                            errMsg += runChls[i]+" ";
                        }
                        MessageBox.Show("第"+(e.RowIndex+1)+"层"+"通道" + errMsg + "正在运行，不允许设置通道锁定");
                    }

                }
                else
                {
                    int mapId = e.ColumnIndex;
                    if (!runChls.Contains(mapId))
                    {
                        DataGridViewCell cell = this.dgvLockInfo.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        cell.Value = Convert.ToString(cell.Value) == "0" ? "1" : "0";//点击值取反
                        DataGridViewCell cellCheck = this.dgvLockInfo.Rows[e.RowIndex].Cells[0];//一编辑单元格就取消全选
                        cellCheck.Value = false;
                    }
                    else
                    {
                        MessageBox.Show("通道运行中，无法改变该通道锁定状态！");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
        }
        /// <summary>
        /// 点击确定按钮保存datagridview到xml文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            string xmlName = "Lock-"+devIp+".xml";
            string xmlPath = DicHelper.RetrieveChlLockPath() + xmlName;//xml文件路径
            if (DgvModifyToXml(xmlPath))//修改成功则更新相应界面的通道锁定
            {
                ChlLock();
            }
            this.Close();//关闭窗口
        }
        /// <summary>
        /// 把datagridview的修改保存到xml
        /// </summary>
        /// <param name="xmlPath"></param>
        private bool DgvModifyToXml(string xmlPath)
        {
            return ChannelLockControl.DgvSaveAsXml(xmlPath, dgvLockInfo,devIp);
        }

        #endregion
        /// <summary>
        /// 加载时清空选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChannelLock_Load(object sender, EventArgs e)
        {
            dgvLockInfo.ClearSelection();
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        #region 国际化
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("ChannelLockTitleText");
            this.lblDevType.Text = ResourceCulture.GetString("DeviceTypeText");
            this.lblDevID.Text = ResourceCulture.GetString("DeviceIDText");
            this.lblUnitId.Text = ResourceCulture.GetString("RowText");
            this.lblChId.Text = ResourceCulture.GetString("ChannelNumText");
            this.btnOK.Text = ResourceCulture.GetString("ChannelLockOkText");
        }
        #endregion
    }
}
